/**
 * Created with IntelliJ IDEA.
 * User: osereda
 * Date: 05.06.13
 */
package com.google.code.bankagregator.adapter.bank.sberrus.reader.credit;

import com.google.code.bankagregator.adapter.exception.AdapterException;
import com.google.code.bankagregator.common.reader.CreditReader;
import com.google.code.bankagregator.entities.Bank;
import com.google.code.bankagregator.entities.Credit;

import java.util.List;

abstract class AbstractCreditSber implements CreditReader {

    private final String BANK_TITLE = "ПАО «СБЕРБАНК РОССИИ»";
    private final String SITE_URL = "http://sberbank.ua/";

    protected final String UNTIL_SELECTOR = "до";

    private Bank bank;
    private String productURL;

    /**
     * @param url string URL of specified product page
     */
    public AbstractCreditSber(String url) {
        bank = new Bank();
        bank.setName(BANK_TITLE);
        bank.setUrl(SITE_URL);
        productURL = url;
    }

    /**
     * @return string URL of current product page
     */
    public String getCreditPageURL() {
        return productURL;
    }

    /**
     * @return Bank VO with data for current product
     */
    public Bank getBank() {
        return bank;
    }

    abstract public List<Credit> getCredits() throws AdapterException;

    /**
     * Read max and min deposit amount string into currency depended array.
     *
     * @param string line with come separated amount values with currency labels.
     * @return String[] with summ and currency code.
     */
    public String[] getAmountFromLine(String string) {
        String line[] = string.split(UNTIL_SELECTOR);
        String codeLine = "";
        for (String str : line) {
            String clearStr = str.replaceAll("[^0-9]+", "").trim();
            codeLine = codeLine + clearStr + "@";
        }
        return codeLine.split("@");
    }
}
